//
//  AnimationsViewController.swift
//  SwiftDemo
//
//  Created by hfzhangzhang on 2018/7/12.
//  Copyright © 2018年 hfzhangzhang. All rights reserved.
//

import UIKit

class AnimationsViewController: UIViewController {

    let viewDemo1:UIView = {
        let viewDemo = UIView(frame: CGRect(x: 10, y: 0, width: 100, height: 50));
        viewDemo.backgroundColor = UIColor.blue;
        return viewDemo;
    }()
    let viewDemo2:UIView = {
        let viewDemo = UIView(frame: CGRect(x: 10, y: 60, width: 100, height: 50));
        viewDemo.backgroundColor = UIColor.blue;
        return viewDemo;
    }()
    let viewDemo3:UIView = {
        let viewDemo = UIView(frame: CGRect(x: 10, y: 110, width: 100, height: 50));
        viewDemo.backgroundColor = UIColor.orange;
        return viewDemo;
    }()
    
    let layerDemo1:CALayer = {
        let layerDemo1 = CALayer.init()
        layerDemo1.frame = CGRect(x: 10, y: 170, width: 100, height: 50)
        layerDemo1.backgroundColor = UIColor.brown.cgColor
        return layerDemo1;
    }()
    
    
    
    

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.yellow;
        self.view.addSubview(viewDemo1)
        self.view.addSubview(viewDemo2)
        self.view.addSubview(viewDemo3)
        
        self.view.layer .addSublayer(layerDemo1)

    }
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.animationDemo1()
        self.animationDemo2()
        self.animationDemo3()
        self.animationDemo4()
        
    }


}

extension AnimationsViewController {
    
    func animationDemo1() {
        // 第一个参数: 动画标识
        // 第二个参数: 附加参数,在设置代理情况下，此参数将发送到setAnimationWillStartSelector和setAnimationDidStopSelector所指定的方法，大部分情况，设置为nil.
        UIView.beginAnimations("animationName", context: nil)
        UIView.setAnimationDelay(1)
        UIView.setAnimationWillStart(#selector(animationDoing))
        UIView.setAnimationDelegate(self)
        UIView.setAnimationRepeatCount(10)
        UIView.setAnimationDelay(0.5)
        UIView.setAnimationRepeatAutoreverses(true);
        
        viewDemo1.transform = CGAffineTransform(scaleX: 1.5, y: 1.5);
        viewDemo1.transform = CGAffineTransform(rotationAngle: CGFloat(Double.pi))
        
        UIView.commitAnimations()
        
    }
    
    func animationDemo2() {
        UIView.beginAnimations("animationName", context: nil)
        UIView.setAnimationDuration(5)
        UIView.setAnimationWillStart(#selector(animationDoing))
        UIView.setAnimationDelegate(self)
        UIView.setAnimationRepeatCount(1)
        UIView.setAnimationDelay(0)
        UIView.setAnimationRepeatAutoreverses(true);
        
        UIView.setAnimationTransition(UIViewAnimationTransition.flipFromLeft, for: viewDemo2, cache: true)

        UIView.commitAnimations()
        
    }
    func animationDemo3() {
        /*
         animateWithDuration 动画持续时间
         delay 动画延迟执行的时间
         usingSpringWithDamping 震动效果，范围0~1，数值越小震动效果越明显
         initialSpringVelocity 初始速度，数值越大初始速度越快
         options 动画的过渡效果
         
         IOS7.0后新增了关键帧动画，支持属性关键帧，不支持路径关键帧
         [UIView animateKeyframesWithDuration:(NSTimeInterval)//动画持续时间
         delay:(NSTimeInterval)//动画延迟执行的时间
         options:(UIViewKeyframeAnimationOptions)//动画的过渡效果
         animations:^{
         //执行的关键帧动画
         }
         completion:^(BOOL finished) {
         //动画执行提交后的操作
         }];
         */
//        UIView.animate(withDuration: 2, delay: 0, usingSpringWithDamping:0.1, initialSpringVelocity: 10, options: UIViewAnimationOptions.allowUserInteraction, animations: {
//            self.viewDemo3.backgroundColor = .red;
//        }) { (true) in
//           zz_printLog("Animationcompletion")
//        }
        
        UIView.transition(from: viewDemo2, to: viewDemo3, duration: 2, options: UIViewAnimationOptions.curveEaseInOut) { (true) in
            
        }
        
        
//        UIView.transition(with: viewDemo3, duration: 3, options: UIViewAnimationOptions.curveEaseInOut, animations: {
//            self.viewDemo3.backgroundColor = .red;
//        }) { (true) in
//
//        }
        
    }
    
    
    @objc func animationDoing(){
       zz_printLog("AnimationWillStart")
    }
    
    
//根层与非根层：
//    每一个UIView内部都默认关联着一个CALayer，我们可用称这个Layer为Root Layer（根层）
//    所有的非Root Layer，也就是手动创建的CALayer对象，都存在着隐式动画
//    当对非Root Layer的部分属性进行修改时，默认会自动产生一些动画效果，而这些属性称为Animatable Properties(可动画属性)。

    func animationDemo4() {
        self.layerDemo1.position = CGPoint(x: 200, y: 150);
        
        //可以通过事务关闭隐式动画
        CATransaction.begin()
        CATransaction.setDisableActions(true)
        self.layerDemo1.backgroundColor = UIColor.black.cgColor;
        CATransaction.commit();


    }
    
}
